//
//  LYSelectCityView.m
//  SelectCity
//
//  Created by liuyan on 2019/3/12.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYSelectCityView.h"
#import "LYSelectCityCell.h"

@interface LYSelectCityView()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, assign) CityState currentState;

//列表
@property (nonatomic, strong) UITableView *leftTable;
@property (nonatomic, strong) UITableView *middleTable;
@property (nonatomic, strong) UITableView *rightTable;

//定位
@property (nonatomic, assign) NSInteger leftIndex;
@property (nonatomic, assign) NSInteger middleIndex;
@property (nonatomic, assign) NSInteger rightIndex;

@end

@implementation LYSelectCityView

- (instancetype)initCityData:(NSArray *)array CityState:(CityState)state{
    self = [super init];
    if (self) {
        self.customizeHeight = 70;
        self.dataArray = array;
        self.currentState = state;
        [self createSubview];
    }
    return self;
}

- (void)createSubview {
    self.frame = CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-self.customizeHeight);
    
    CGFloat width = (self.currentState == CityStateSecondary) ? WIDTH/2 : WIDTH/3;
    
    self.leftTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-self.customizeHeight) style:UITableViewStyleGrouped];
    self.leftTable.delegate = self;
    self.leftTable.dataSource = self;
    self.leftTable.showsVerticalScrollIndicator = NO;
    self.leftTable.estimatedRowHeight = 0;
    self.leftTable.estimatedSectionHeaderHeight = 0;
    self.leftTable.estimatedSectionFooterHeight = 0;
    self.leftTable.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.leftTable];
    self.leftIndex = 0;
    
    self.middleTable = [[UITableView alloc] initWithFrame:CGRectMake(width, 0, width, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-self.customizeHeight) style:UITableViewStyleGrouped];
    self.middleTable.delegate = self;
    self.middleTable.dataSource = self;
    self.middleTable.showsVerticalScrollIndicator = NO;
    self.middleTable.estimatedRowHeight = 0;
    self.middleTable.estimatedSectionHeaderHeight = 0;
    self.middleTable.estimatedSectionFooterHeight = 0;
    self.middleTable.backgroundColor = COLOR_OF_WHITE;
    [self addSubview:self.middleTable];
    self.middleIndex = 0;
    
    [self.leftTable registerClass:[LYSelectCityCell class] forCellReuseIdentifier:kCellIdentifier_LYSelectCityCell];
    [self.middleTable registerClass:[LYSelectCityCell class] forCellReuseIdentifier:kCellIdentifier_LYSelectCityCell];
    
    if (self.currentState == CityStateThird) {
        self.rightTable = [[UITableView alloc] initWithFrame:CGRectMake(2*width, 0, width, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-self.customizeHeight) style:UITableViewStyleGrouped];
        self.rightTable.delegate = self;
        self.rightTable.dataSource = self;
        self.rightTable.showsVerticalScrollIndicator = NO;
        self.rightTable.estimatedRowHeight = 0;
        self.rightTable.estimatedSectionHeaderHeight = 0;
        self.rightTable.estimatedSectionFooterHeight = 0;
        self.rightTable.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.rightTable];
        
        [self.rightTable registerClass:[LYSelectCityCell class] forCellReuseIdentifier:kCellIdentifier_LYSelectCityCell];
        self.rightIndex = 0;
    }
}
#pragma mark -- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.leftTable) {
        return self.dataArray.count;
    } else if(tableView == self.middleTable) {
        NSDictionary *secDic = self.dataArray[self.leftIndex];
        NSArray *secArray = secDic[@"child"];
        return secArray.count;
    } else {
        NSDictionary *secDic = self.dataArray[self.leftIndex];
        NSArray *secArray = secDic[@"child"];
        NSDictionary *thirdDic = secArray[self.middleIndex];
        NSArray *thirdArray = thirdDic[@"child"];
        return thirdArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LYSelectCityCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYSelectCityCell forIndexPath:indexPath];
    BOOL isSecond = (self.currentState == CityStateSecondary) ? YES : NO;
    
    if (tableView == self.leftTable) {
        [cell setData:self.dataArray[indexPath.row] isCityStateSecond:isSecond isSelect:(self.leftIndex == indexPath.row)? YES : NO];
    } else if(tableView == self.middleTable) {
        NSDictionary *secDic = self.dataArray[self.leftIndex];
        NSArray *secArray = secDic[@"child"];
        [cell setData:secArray[indexPath.row] isCityStateSecond:isSecond isSelect:(self.middleIndex == indexPath.row)? YES : NO];
    } else {
        NSDictionary *secDic = self.dataArray[self.leftIndex];
        NSArray *secArray = secDic[@"child"];
        NSDictionary *thirdDic = secArray[self.middleIndex];
        NSArray *thirdArray = thirdDic[@"child"];
        [cell setData:thirdArray[indexPath.row] isCityStateSecond:isSecond isSelect:(self.rightIndex == indexPath.row)? YES : NO];
    }
    return cell;
}
#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTable) {
        if (indexPath.row != self.leftIndex) {
            self.leftIndex = indexPath.row;
            self.middleIndex = 0;
            [self.leftTable reloadData];
            [self.middleTable reloadData];
            if (self.currentState == CityStateThird) {
                self.rightIndex = 0;
                [self.rightTable reloadData];
            }
        }
    } else if(tableView == self.middleTable) {
        if (indexPath.row != self.middleIndex) {
            self.middleIndex = indexPath.row;
            [self.middleTable reloadData];
            if (self.currentState == CityStateThird) {
                self.rightIndex = 0;
                [self.rightTable reloadData];
            }
        }
    } else {
        self.rightIndex = indexPath.row;
        [self.rightTable reloadData];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 45.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark -- Action
- (NSDictionary *)getProvince {
    NSDictionary *Province = self.dataArray[self.leftIndex];
    return Province;
}
- (NSDictionary *)getCity {
    NSDictionary *Province = self.dataArray[self.leftIndex];
    NSArray *cityArray = Province[@"child"];
    NSDictionary *City = cityArray[self.middleIndex];
    return City;
}
- (NSDictionary *)getArea {
    NSDictionary *Province = self.dataArray[self.leftIndex];
    NSArray *cityArray = Province[@"child"];
    NSDictionary *City = cityArray[self.middleIndex];
    NSArray *areaArray = City[@"child"];
    NSDictionary *Area = areaArray[self.middleIndex];
    return Area;
}
- (NSDictionary *)getAllCityInfo {
    NSDictionary *Province = self.dataArray[self.leftIndex];
    NSArray *cityArray = Province[@"child"];
    NSDictionary *City = cityArray[self.middleIndex];
    NSMutableDictionary *cityInfo = [NSMutableDictionary dictionary];
    cityInfo[@"province"] = Province[@"name"];
    cityInfo[@"provinceid"] = Province[@"id"];
    cityInfo[@"city"] = City[@"name"];
    cityInfo[@"cityid"] = City[@"id"];
    if (self.currentState == CityStateThird) {
        NSArray *areaArray = City[@"child"];
        if (areaArray.count) {
            NSDictionary *Area = areaArray[self.rightIndex];
            cityInfo[@"area"] = Area[@"name"];
            cityInfo[@"areaid"] = Area[@"id"];
        }else{
            cityInfo[@"area"] = @"";
            cityInfo[@"areaid"] = @"";
        }
    }
    return cityInfo;
}

@end
